/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package producer;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.ejb.Schedule;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import services.MessageInterface;

/**
 *
 * @author ceu
 */
@Stateless
@LocalBean
public class producer implements MessageInterface {
    @Resource(mappedName = "jms/LogisticQueue")
    private Queue logisticQueue;
    @Resource(mappedName = "jms/LogisticFactory")
    private ConnectionFactory factory;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    private Message createJMSMessageForjmsLogisticQueue(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        TextMessage tm = session.createTextMessage();
        tm.setText(messageData.toString());
        return tm;
    }

    
    @Override
    public void sendShipmentMessageToLogisticQueue() throws JMSException {
        Connection connection = null;
        Session session = null;
        Object messageData = "1;123;0.14;100;91876542;912343212;28413;madrid;barrero;el boalo;24502;miranda;caca;zaragoza";
        try {
            connection = factory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(logisticQueue);
            messageProducer.send(createJMSMessageForjmsLogisticQueue(session, messageData));
            System.out.println("Producer ha enviado Shipment mensaje");
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
    


@Override
    public void sendPriceMessageToLogisticQueue() throws JMSException {
        Connection connection = null;
        Session session = null;
        Object messageData = "2;123;28413;24502;0.2;100";
        try {
            connection = factory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(logisticQueue);
            messageProducer.send(createJMSMessageForjmsLogisticQueue(session, messageData));
            System.out.println("Producer ha enviado Price mensaje");
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }


@Override
    public void sendStatusMessageToLogisticQueue() throws JMSException {
        Connection connection = null;
        Session session = null;
        Object messageData = "3;34;43";
        try {
            connection = factory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(logisticQueue);
            messageProducer.send(createJMSMessageForjmsLogisticQueue(session, messageData));
            System.out.println("Producer ha enviado Status mensaje");
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
    





}